//
//  UIButton+Config.m
//  LSControl
//
//  Created by Beyondream on 2018/5/9.
//  Copyright © 2018年 Beyondream. All rights reserved.
//

#import "UIButton+Config.h"
#import <objc/runtime.h>
static const NSString * LSClickHandleKey = @"LSClickHandleKey";
@implementation UIButton (Config)
/**
 button type
 */
+ (UIButton *(^)(UIButtonType buttonType))byd_buttonType {
    return ^UIButton * (UIButtonType buttonType) {
        return [UIButton buttonWithType:buttonType];
    };
}

/**
 default is UIEdgeInsetsZero.
 */
- (UIButton * (^)(UIEdgeInsets contentEdgeInsets))byd_contentEdgeInsets {
    return ^UIButton * (UIEdgeInsets contentEdgeInsets) {
         [self setContentEdgeInsets:contentEdgeInsets];
         return self;
    };
}

/**
 default is UIEdgeInsetsZero
 */
- (UIButton * (^)(UIEdgeInsets titleEdgeInsets))byd_titleEdgeInsets {
    return ^UIButton * (UIEdgeInsets titleEdgeInsets) {
        [self setTitleEdgeInsets:titleEdgeInsets];
        return self;
    };
}

/**
 default is UIEdgeInsetsZero
 */
- (UIButton * (^)(UIEdgeInsets imageEdgeInsets))byd_imageEdgeInsets {
    return ^UIButton * (UIEdgeInsets imageEdgeInsets) {
        [self setImageEdgeInsets:imageEdgeInsets];
        return self;
    };
}

/**
 default is YES. if YES, image is drawn darker when highlighted(pressed)
 */
- (UIButton * (^)(BOOL  adjustsImageWhenHighlighted))byd_adjustsImageWhenHighlighted {
    return ^UIButton * (BOOL  adjustsImageWhenHighlighted) {
        self.adjustsImageWhenHighlighted = adjustsImageWhenHighlighted;
        return self;
    };
}


/**
 default is YES. if YES, image is drawn lighter when disabled
 */
- (UIButton * (^)(BOOL  adjustsImageWhenDisabled))byd_adjustsImageWhenDisabled {
    return ^UIButton * (BOOL  adjustsImageWhenDisabled) {
        self.adjustsImageWhenDisabled = adjustsImageWhenDisabled;
        return self;
    };
}

/**
 default is NO. if YES, show a simple
 */
- (UIButton * (^)(BOOL  showsTouchWhenHighlighted))byd_showsTouchWhenHighlighted {
    return ^UIButton * (BOOL  showsTouchWhenHighlighted) {
        self.showsTouchWhenHighlighted = showsTouchWhenHighlighted;
        return self;
    };
}


/**
 The tintColor is inherited through the superview hierarchy. See UIView for more information
 */
- (UIButton * (^)(UIColor *tintColor))byd_tintColor {
    return ^UIButton * (UIColor *tintColor) {
        self.tintColor = tintColor;
        return self;
    };
}
/**
 default is nil. title is assumed to be single line
 */
- (UIButton * (^)(NSString * title , UIControlState state))byd_title {
    return ^UIButton * (NSString * title , UIControlState state) {
        [self setTitle:title forState:state];
        return self;
    };
}
/**
 default is nil. use 50% black
 */
- (UIButton * (^)(UIColor * titleShadowColor , UIControlState state))byd_titleShadowColor {
    return ^UIButton * (UIColor * titleShadowColor , UIControlState state) {
        [self setTitleShadowColor:titleShadowColor forState:state];
        return self;
    };
}


- (UIButton * (^)(UIColor * textColor , UIControlState state))byd_titleColor {
    return ^UIButton * (UIColor * textColor , UIControlState state) {
        [self setTitleColor:textColor forState:state];
        return self;
    };
}
/**
 default is nil. should be same size if different for different states
 */
- (UIButton * (^)(UIImage * image , UIControlState state))byd_image {
    return ^UIButton * (UIImage * image , UIControlState state) {
        [self setImage:image forState:state];
        return self;
    };
}

/**
 default is nil
 */
- (UIButton * (^)(UIImage * image , UIControlState state))byd_backgroundImage {
    return ^UIButton * (UIImage * image , UIControlState state) {
        [self setBackgroundImage:image forState:state];
        return self;
    };
}
/**
 default is nil. title is assumed to be single line
 */
- (UIButton * (^)(NSAttributedString * title ,UIControlState state))byd_attributedTitle {
    return ^UIButton * (NSAttributedString * title , UIControlState state) {
        [self setAttributedTitle:title forState:state];
        return self;
    };
}

/**
 default is 17.
 */
- (UIButton * (^)(UIFont *font))byd_titleFont {
    return ^UIButton * (UIFont *font) {
        self.titleLabel.font = font;
        return self;
    };
}

/**
 tag
 */
- (UIButton * (^)(NSInteger tag))byd_tag {
    return ^UIButton * (NSInteger tag) {
        self.tag = tag;;
        return self;
    };
}

/**
 default is white.
 */
- (UIButton * (^)(UIColor * backgroundColor))byd_backgroundColor {
    return ^UIButton * (UIColor * backgroundColor) {
        [self setBackgroundColor:backgroundColor];
        return self;
    };
}

- (UIButton * (^)(CGFloat cornerRadius))byd_cornerRadius {
    return ^UIButton * (CGFloat cornerRadius) {
        self.layer.cornerRadius = cornerRadius;
        self.clipsToBounds = YES;
        return self;
    };
}

- (UIButton * (^)(CGFloat borderWidth))byd_borderWidth {
    return ^UIButton * (CGFloat borderWidth) {
        self.layer.borderWidth = borderWidth;
        self.clipsToBounds = YES;
        return self;
    };
}

- (UIButton * (^)(UIColor * borderColor))byd_borderColor {
    return ^UIButton * (UIColor * borderColor) {
        self.layer.borderColor = borderColor.CGColor;
        return self;
    };
}
/**
 reset button Subviews
 */
- (UIButton * (^)(CGFloat space))titleOverImage {
    return  ^UIButton *(CGFloat space) {
        [self judgeTheTitleInImageTop:YES space:space];
        return self;
    };
}
- (UIButton * (^)(CGFloat space))titleBelowImage{
    return  ^UIButton *(CGFloat space) {
        [self judgeTheTitleInImageTop:NO space:space];
        return self;
    };
}
- (UIButton * (^)(CGFloat space))imageOnTheTitleLeft{
    return  ^UIButton *(CGFloat space) {
        [self resetEdgeInsets];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space, 0, -space)];
        [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
        return self;
    };
}
- (UIButton * (^)(CGFloat space))imageOnTheTitleRight{
    return  ^UIButton *(CGFloat space) {
        [self resetEdgeInsets];
        [self setNeedsLayout];
        [self layoutIfNeeded];
        
        CGRect contentRect = [self contentRectForBounds:self.bounds];
        CGSize titleSize = [self titleRectForContentRect:contentRect].size;
        CGSize imageSize = [self imageRectForContentRect:contentRect].size;
        
        [self setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, space)];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageSize.width, 0, imageSize.width)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleSize.width+space, 0, -titleSize.width - space)];
        return self;
    };
}


//重置内边距
- (void)resetEdgeInsets
{
    [self setContentEdgeInsets:UIEdgeInsetsZero];
    [self setImageEdgeInsets:UIEdgeInsetsZero];
    [self setTitleEdgeInsets:UIEdgeInsetsZero];
}

/**  判断标题是不是在上   */
- (void)judgeTheTitleInImageTop:(BOOL)isTop space:(float)space ;
{
    [self resetEdgeInsets];
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    CGRect contentRect = [self contentRectForBounds:self.bounds];
    CGSize titleSize = [self titleRectForContentRect:contentRect].size;
    CGSize imageSize = [self imageRectForContentRect:contentRect].size;
    
    float halfWidth = (titleSize.width + imageSize.width)/2;
    float halfHeight = (titleSize.height + imageSize.height)/2;
    
    float topInset = MIN(halfHeight, titleSize.height);
    float leftInset = (titleSize.width - imageSize.width)>0?(titleSize.width - imageSize.width)/2:0;
    float bottomInset = (titleSize.height - imageSize.height)>0?(titleSize.height - imageSize.height)/2:0;
    float rightInset = MIN(halfWidth, titleSize.width);
    
    if (isTop) {
        [self setTitleEdgeInsets:UIEdgeInsetsMake(-titleSize.height-space, - halfWidth, imageSize.height+space, halfWidth)];
        [self setContentEdgeInsets:UIEdgeInsetsMake(topInset+space, leftInset, -bottomInset, -rightInset)];
    } else {
        [self setTitleEdgeInsets:UIEdgeInsetsMake(imageSize.height+space, - halfWidth, -titleSize.height-space, halfWidth)];
        [self setContentEdgeInsets:UIEdgeInsetsMake(-bottomInset, leftInset, topInset+space, -rightInset)];
    }
}

/**
 addActionHandle
 */
- (UIButton * (^)(UIControlEvents event,ActionHandle handle))byd_addActionHandle{
    
    return ^UIButton * (UIControlEvents event,ActionHandle handle) {
        objc_setAssociatedObject(self, &LSClickHandleKey, handle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self addTarget:self action:@selector(lsClick:) forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}

/**
 addActionHandle 有参数
 */
- (UIButton * (^)(UIControlEvents event,ActionParamHandle handle))byd_addParamActionHandle {
    return ^UIButton * (UIControlEvents event,ActionParamHandle handle) {
        objc_setAssociatedObject(self, &LSClickHandleKey, handle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self addTarget:self action:@selector(lsParamClick:) forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}
/** 无参数点击事件 **/
- (void)lsClick:(UIButton *)sender {
    void (^handleBlock)(void)  = objc_getAssociatedObject(self, &LSClickHandleKey);
    if (handleBlock) {
        handleBlock();
    }
    
}
/** 有参数点击事件 **/
- (void)lsParamClick:(UIButton *)sender {
    void (^handleBlock)(UIButton * sender)  = objc_getAssociatedObject(self, &LSClickHandleKey);
    if (handleBlock) {
        handleBlock(sender);
    }
    
}
@end











